package com.neuedu.javase.tools.date;

import com.neuedu.javase.oo.basics.C;

import java.util.Calendar;
import java.util.Date;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.tools.date 
 * date:   2025/10/30   14:05  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class CompareTime {


    public static void main(String[] args) {

        //Date
        Date d1 = new Date();
        Date d2 = new Date();
        Date d3 = new Date(2023-1900, 2 , 30);



        //比较两个Date
        System.out.println("d1.getTime() = " + d1.getTime());
        System.out.println("d2.getTime() = " + d2.getTime());
        System.out.println("d3.getTime() = " + d3.getTime());


        System.out.println("====================before、after   ========");
        System.out.println("d1.before(d2) = " + d1.before(d2));  //false
        System.out.println("d3.before(d2) = " + d3.before(d2));  //true

        System.out.println("d1.after(d2) = " + d1.after(d2));   //false
        System.out.println("d3.after(d2) = " + d3.after(d2));  //false

        System.out.println("====================compareTo===   ========");

        System.out.println("d1.compareTo(d2) = " + d1.compareTo(d2));  // 0
        System.out.println("d3.compareTo(d2) = " + d3.compareTo(d2));  //d3 < d2  -1
        System.out.println("d2.compareTo(d3) = " + d2.compareTo(d3));  //d2  >  d3  +1


        // Number String

        Calendar c1 = Calendar.getInstance();

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.YEAR,2017);


        formatOutput(c1);
        formatOutput(c2);
        System.out.println("c1.before(c2) = " + c1.before(c2));  //false
        System.out.println("c2.before(c1) = " + c2.before(c1));  //true


    }


    /**
     * 格式化输出
     * @param calendar
     * @return
     */
    public static  void  formatOutput(Calendar calendar){
        //     获取字段
        int year = calendar.get(Calendar.YEAR);
        //月份从0 开始  0--11
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        int hour = calendar.get(Calendar.HOUR);   //12 小时
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);  //24 时

        // CTRL+SHIFT+ U 切换大小写
        int minutes = calendar.get(Calendar.MINUTE);  //24 时
        int seconds = calendar.get(Calendar.SECOND);  //24 时

        String format =  year+"-"+month+"-"+dayOfMonth+" "+hourOfDay +":"+minutes+":"+seconds+"  星期"+dayOfWeek;
        System.out.println(format);

    }


}
